package com.ruoyi.project.ins.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ins.domain.InsBatchMessage;
import com.ruoyi.project.ins.service.IInsBatchMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ins/batchMessage")
public class InsBatchMessageController extends BaseController {
    @Autowired
    private IInsBatchMessageService insBatchMessageService;

    @PreAuthorize("@ss.hasPermi('ins:batch:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsBatchMessage batchMessage)
    {
        String username = getUsername();
        if (!username.equalsIgnoreCase("admin")) {
            batchMessage.setCreateBy(username);
        }
        startPage();
        List<InsBatchMessage> list = insBatchMessageService.selectBatchMessageList(batchMessage);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ins:batch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(insBatchMessageService.selectBatchMessageById(id));
    }

//    @PreAuthorize("@ss.hasPermi('ins:batch:edit')")
//    @Log(title = "ins消息管理", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@Validated @RequestBody InsBatchMessage batchMessage)
//    {
//        batchMessage.setUpdateBy(getUsername());
//        return toAjax(batchMessageService.updateInsBatchMessage(batchMessage));
//    }

    @Log(title = "ins消息管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('ins:batch:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsBatchMessage batchMessage)
    {
        String username = getUsername();
        if (!username.equalsIgnoreCase("admin")) {
            batchMessage.setCreateBy(username);
        }
        List<InsBatchMessage> list = insBatchMessageService.selectBatchMessageList(batchMessage);
        ExcelUtil<InsBatchMessage> util = new ExcelUtil<>(InsBatchMessage.class);
        util.exportExcel(response, list, "批量消息数据");
    }
}
